<template>
  <el-form :model="tableForm" :rules="tableRule" ref="tableForm" label-width="100px">

    <el-form-item label="学号" prop="account">
      <el-input size="small" v-model="tableForm.account" auto-complete="off" placeholder="请输入学号"></el-input>
    </el-form-item>

    <el-form-item label="姓名" prop="name">
      <el-input size="small" v-model="tableForm.name" auto-complete="off" placeholder="请输入姓名"></el-input>
    </el-form-item>

    <el-form-item label="密码" prop="password">
      <el-input size="small" type="password" v-model="tableForm.password" auto-complete="off" placeholder="请输入密码"></el-input>
    </el-form-item>

    <el-form-item label="性别" prop="sex">
      <el-radio-group v-model="tableForm.sex">
        <el-radio :label="0">男</el-radio>
        <el-radio :label="1">女</el-radio>
      </el-radio-group>
    </el-form-item>

    <el-form-item label="手机号" prop="phone">
      <el-input size="small" v-model="tableForm.phone" auto-complete="off" placeholder="请输入手机号"></el-input>
    </el-form-item>

    <el-form-item label="邮箱" prop="email">
      <el-input size="small" v-model="tableForm.email" auto-complete="off" placeholder="请输入邮箱"></el-input>
    </el-form-item>

    <el-form-item label="学校" prop="school">
      <el-input size="small" v-model="tableForm.school" auto-complete="off" placeholder="请输入学校"></el-input>
    </el-form-item>

    <el-form-item label="学院" prop="college">
      <el-input size="small" v-model="tableForm.college" auto-complete="off" placeholder="请输入学院"></el-input>
    </el-form-item>

    <el-form-item label="班级" prop="grade">
      <el-input size="small" v-model="tableForm.grade" auto-complete="off" placeholder="请输入班级"></el-input>
    </el-form-item>

    <el-form-item>
      <el-button size="small" type="primary"  @click="submitForm()">保存</el-button>
    </el-form-item>


  </el-form>
</template>

<script>

import {addData, updateData, getData} from '@/api/user'
import store from '@/store';

// 表单属性
const tableForm = {
  id: '',
  account: '',
  name: '',
  password: '',
  phone: '',
  sex: 0,
  email: '',
  school: '',
  college: '',
  grade: '',
  role: '学生'
}

// 表单校验
const tableRule = {
  account: [{ required: true, message: '请输入学号', trigger: 'blur' }],
  name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]

};

export default {
  name: 'UserDetail',
  data() {
    return {
      tableForm: tableForm,
      tableRule: tableRule
    }
  },
  components: {
  },
  created() {
    this.initUserData()
  },
  methods:{
    initUserData(){
      getData({id: store.getters.avatar }).then(res => {
        let row = res.body
        this.tableForm.id = row.id
        this.tableForm.account = row.account
        this.tableForm.name = row.name
        this.tableForm.password = row.password
        this.tableForm.phone = row.phone
        this.tableForm.sex = row.sex
        this.tableForm.email = row.email
        this.tableForm.school = row.school
        this.tableForm.college = row.college
        this.tableForm.grade = row.grade
        this.tableForm.role = row.role

      })
    },
    //表单保存事件
    submitForm() {

      this.$refs["tableForm"].validate(valid => {
        if (valid) {
          if(this.tableForm.id == ""){
            addData(this.tableForm).then(res => {
              if (res.message == "success" ) {
                this.$message({
                  type: 'success',
                  message: '新增成功！'
                });
                this.$emit("closeDialog", false);
              } else {
                this.$message({
                  type: 'info',
                  message: res.message
                });
              }
            });
          }else{
            updateData(this.tableForm).then(res => {
              if (res.message == "success" ) {
                this.$message({
                  type: 'success',
                  message: '修改成功！'
                });
                this.$emit("closeDialog", false);
              } else {
                this.$message({
                  type: 'info',
                  message: res.message
                });
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    clearForm(){
      this.$refs.tableForm.resetFields();
      this.$refs.tableForm.clearValidate();
    },
    setAvatar(code){
      this.tableForm.picture = code
      console.log(this.tableForm.picture )
    }
  }
}
</script>

<style scoped>


</style>
